import { getToken, removeToken, setToken } from '@/utils/auth'
import { getId, getInfo, login } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, token) {
      state.token = token
      setToken(token)
    },
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    removeToken(state) {
      state.token = ''
      removeToken()
    },
    removeUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async  userLogin(context, loginForm) {
      const res = await login(loginForm)
      // console.log(context, loginForm)
      // console.log('请求正确', res)
      context.commit('updateToken', res.data)
      return res
    },
    async getUserInfo(context) {
      const res = await getInfo()
      // console.log('用户信息', res)

      const res1 = await getId(res.data.userId)
      // console.log(res)

      context.commit('updateUserInfo', { ...res.data, ...res1.data })
    },
    logout(context) {
      context.commit('removeToken')
      context.commit('removeUserInfo')
    }

  }
}
